package com.bkgy.modularbx.theme.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.commom.model.QueryDTO;
import com.bkgy.config.BusinessException;
import com.bkgy.modularbx.theme.domain.ThemeData;
import com.bkgy.modularbx.theme.dto.ThemeDataDto;
import com.bkgy.modularbx.theme.mapper.ThemeDataMapper;
import com.bkgy.modularbx.theme.service.ThemeDataService;
import com.bkgy.sys.common.entity.ResultData;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * 主题数据服务实现类
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class ThemeDataServiceImpl extends ServiceImpl<ThemeDataMapper, ThemeData> implements ThemeDataService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultData<Void> addThemeData(ThemeData themeData) {
        themeData.setToc(new Date());
        themeData.setTom(new Date());
        save(themeData);
        return ResultData.ResultDataSuccessSelf("添加成功", null);
    }

    @Override
    public ResultData<Page<ThemeData>> pageThemeData(QueryDTO<ThemeDataDto> queryDTO) {
        ThemeDataDto dto = queryDTO.getDto();
        LambdaQueryWrapper<ThemeData> wrapper = new LambdaQueryWrapper<>();

        if (dto != null) {
            wrapper.eq(StringUtils.hasText(dto.getThemeGroupId()), ThemeData::getThemeGroupId, dto.getThemeGroupId());
            wrapper.like(StringUtils.hasText(dto.getThemeVariable()), ThemeData::getThemeVariable, dto.getThemeVariable());
            wrapper.like(StringUtils.hasText(dto.getThemeVariableDescription()), ThemeData::getThemeVariableDescription, dto.getThemeVariableDescription());
        }

        Page<ThemeData> page = new Page<>(queryDTO.getPage().getPageIndex(), queryDTO.getPage().getPageSize());
        Page<ThemeData> result = page(page, wrapper);
        return ResultData.ResultDataSuccess(result.getRecords(), (int) result.getTotal());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultData<Void> deleteThemeData(String themeGroupId, String themeVariable) {
        LambdaQueryWrapper<ThemeData> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ThemeData::getThemeGroupId, themeGroupId);
        wrapper.eq(ThemeData::getThemeVariable, themeVariable);
        ThemeData themeData = getOne(wrapper);
        if (themeData == null) {
            throw new BusinessException("要删除的主题数据不存在");
        }
        boolean remove = remove(wrapper);
        return remove ? ResultData.ResultDataSuccessSelf("删除成功", null) : ResultData.ResultDataFaultSelf("删除失败", null);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResultData<Void> updateThemeData(ThemeData themeData) {
        LambdaQueryWrapper<ThemeData> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ThemeData::getThemeGroupId, themeData.getThemeGroupId());
        wrapper.eq(ThemeData::getThemeVariable, themeData.getThemeVariable());
        ThemeData existThemeData = getOne(wrapper);
        if (existThemeData == null) {
            throw new BusinessException("要修改的主题数据不存在");
        }
        themeData.setTom(new Date());
        boolean update = update(themeData, wrapper);
        return update ? ResultData.ResultDataSuccessSelf("修改成功", null) : ResultData.ResultDataFaultSelf("修改失败", null);
    }

    @Override
    public ResultData<ThemeData> findById(String themeGroupId, String themeVariable) {
        LambdaQueryWrapper<ThemeData> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ThemeData::getThemeGroupId, themeGroupId);
        wrapper.eq(ThemeData::getThemeVariable, themeVariable);
        ThemeData themeData = getOne(wrapper);
        if (themeData == null) {
            throw new BusinessException("主题数据不存在");
        }
        return ResultData.ResultDataSuccess(themeData, 1);
    }
}




